package com.itheima.controller.backend.controller;

import com.itheima.common.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/ordersetting")
public class ordersettingController {

    @Autowired
    private OrderSettingService orderSettingService;

    @GetMapping("/download")
    public void download(HttpServletResponse response, String fileName) {
        try {
/*
            File f = new File("src/main/resources/templates/" + fileName);

            // 设置响应数据需保存为文件，同时指定下载信息。
            response.setContentType("audio/mp3");
            response.setContentLengthLong(f.length());
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("UTF-8", f.getName()));
*/

            XSSFWorkbook excel = new XSSFWorkbook(this.getClass().getClassLoader().getResourceAsStream("templates/ordersetting_template.xlsx"));

            response.setHeader("Content-disposition", "attachment;filename*=utf-8''ordersetting_template.xlsx");
            response.setContentType("application/x-xls");

            excel.write(response.getOutputStream());

            excel.close();
            response.getOutputStream().close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @PostMapping("/upload")
    public Result upload(MultipartFile excelFile) {
        orderSettingService.upload(excelFile);
        return new Result(true, "预约设置成功", null);
    }

    @PostMapping("/editNumberByOrderDate")
    public Result editNumberByOrderDate(@RequestBody OrderSetting orderSetting) {
        try {
            orderSettingService.editReservationsByOrderDate(orderSetting);
            return new Result(true, "预约设置成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "预约设置失败", null);
        }
    }

    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(@RequestParam("month") String orderDate) {
        List<OrderSetting> orderSettingList = orderSettingService.getOrderSettingByMonth(orderDate);

        if (orderSettingList != null && orderSettingList.size() > 0) {
            for (OrderSetting orderSetting : orderSettingList) {
                Date dateDate = orderSetting.getOrderDate();
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
                String date = sdf1.format(dateDate);
                orderSetting.setDate(Integer.valueOf((date.substring(date.lastIndexOf("-"), date.length() - 1))));
            }
        }
        return new Result(true, "获取预约设置数据成功", orderSettingList);
    }

}
